<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 面包屑 -->
        <div slot="header" class="clearfix" v-if="subjects.id && subjects.name">
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>学科管理</el-breadcrumb-item>
            <el-breadcrumb-item>{{ subjects.id ? subjects.name : '' }}</el-breadcrumb-item>
            <el-breadcrumb-item>目录管理</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <!-- 头部 -->
        <div class="top">
          <el-form :inline="true">
            <el-form-item label="目录名称">
              <el-input size="small" v-model="form.directoryName" />
            </el-form-item>
            <el-form-item label="状态" style="margin-left: 30px">
              <el-select size="small" v-model="form.state">
                <el-option :value="item.value" :label="item.label" v-for="item in state" :key="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="del">清除</el-button>
              <el-button size="small" type="primary" @click="search">搜索</el-button>
            </el-form-item>
          </el-form>
          <!-- 右侧按钮 -->
          <div class="right">
            <el-link
              v-if="subjects.id && subjects.name"
              type="primary"
              class="link"
              icon="el-icon-back"
              :underline="false"
              @click="$router.push('/subjects/list')"
              >返回学科</el-link
            >
            <el-button size="small" type="success" icon="el-icon-edit" @click="add">新增目录</el-button>
          </div>
        </div>
        <!-- 警告 -->

        <el-tag type="info" style="width:100%;margin-top: -5px;height:40px;line-height:40px">
          <i class="el-icon-info" style="margin-right:5px"></i>
          <span>数据一共 {{ total }} 条</span>
        </el-tag>
        <!-- <el-alert :closable="false" show-icon type="info" style="margin-top: -5px"> 数据一共 {{ total }} 条</el-alert> -->
        <!-- 表格 -->
        <el-table :data="list" max-height="660px" style="margin-top: 15px">
          <el-table-column type="index" label="序号"></el-table-column>
          <el-table-column label="所属学科" prop="subjectName"></el-table-column>
          <el-table-column label="目录名称" prop="directoryName"></el-table-column>
          <el-table-column label="创建者" prop="username"></el-table-column>
          <el-table-column label="创建日期" prop="addDate">
            <template v-slot="{ row }">
              <span>{{ row.addDate | dateFilter }}</span>
            </template>
          </el-table-column>
          <el-table-column label="面试数量" prop="totals"></el-table-column>
          <el-table-column label="状态">
            <template v-slot="{ row }">
              <span v-if="row.state === 1">已启用</span>
              <span v-if="row.state === 0">已禁用</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150px">
            <template v-slot="{ row }">
              <div class="tag">
                <el-link type="primary" v-if="row.state === 1" @click="disabled(row.id, row.state)" :underline="false"
                  >禁用</el-link
                >
                <el-link type="primary" v-if="row.state === 0" @click="disabled(row.id, row.state)" :underline="false"
                  >启用</el-link
                >
                <el-link
                  type="info"
                  v-if="row.state === 1"
                  @click="edit(row.id)"
                  :underline="false"
                  :disabled="row.state === 1"
                  >修改</el-link
                >
                <el-link type="primary" v-else @click="edit(row.id)" :underline="false" :disabled="row.state === 1"
                  >修改</el-link
                >
                <el-link
                  type="info"
                  v-if="row.state === 1"
                  @click="remove(row.id)"
                  :underline="false"
                  :disabled="row.state === 1"
                  >删除</el-link
                >
                <el-link type="primary" v-else @click="remove(row.id)" :underline="false" :disabled="row.state === 1"
                  >删除</el-link
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          v-if="!subjects.id || !subjects.name"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="q.page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="q.pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="total"
        >
        </el-pagination>
      </el-card>
      <!-- 添加按钮组件 -->
      <DirectorysAdd :showDialog.sync="isShowDialog" :array="simpleList" @reload="initList" />
      <!-- 修改组件 -->
      <DirectorysEdit :showDialog.sync="isShowEdit" :id.sync="id" :array="simpleList" @reload="initList" />
    </div>
  </div>
</template>

<script>
import { list as getCompanys } from '@/api/hmmm/companys'
import { list as getQuestion } from '@/api/hmmm/questions'
// 按需导入接口
import { list, changeState, simple, remove } from '@/api/hmmm/directorys.js'
// 按需导入常量
import { status } from '@/api/hmmm/constants.js'
// 导入dayjs
import dayjs from 'dayjs'
// 导入组件
import DirectorysAdd from '@/module-hmmm/components/directorys-add'
import DirectorysEdit from '@/module-hmmm/components/DirectorysEdit'
export default {
  name: 'Directirys',
  components: {
    DirectorysAdd,
    DirectorysEdit
  },
  data() {
    return {
      q: {
        page: 1, // 否 1 当前页数
        pagesize: 10 // 否 10 页尺寸
      },
      total: 0,
      list: [],
      // 头部
      state: status,
      // 头部
      form: {
        directoryName: '',
        state: ''
      },
      // 是否展示添加组价
      isShowDialog: false,
      // 是否展示修改组件
      isShowEdit: false,
      // 修改id
      id: '',
      // 简单列表
      simpleList: []
    }
  },
  created() {
    getCompanys()
    getQuestion()
    this.initList()
    this.initSimpleList()
  },
  computed: {
    subjects() {
      return this.$route.query || null
    }
  },
  watch: {
    '$route.query'() {
      this.initList()
    }
  },
  methods: {
    // 初始化目录列表
    async initList() {
      const res = await list({ ...this.q, subjectID: this.subjects.id || undefined })
      this.list = res.data.items
      this.total = res.data.counts
      // console.log(res)
    },
    // 初始化简单列表数据
    async initSimpleList() {
      const res = await simple()
      this.simpleList = res.data
      // console.log(res)
    },
    handleSizeChange(newPageSize) {
      this.q.pagesize = newPageSize
      this.initList()
    },
    handleCurrentChange(newPage) {
      this.q.page = newPage
      this.initList()
    },
    // 清除按钮事件
    del() {
      this.form.directoryName = ''
      this.form.state = ''
    },
    // 搜索
    async search() {
      const res = await list({ ...this.q, state: this.form.state || undefined, directoryName: this.form.directoryName })
      this.list = res.data.items
      console.log(this.list)
      this.total = res.data.counts
      // console.log(res)
    },
    // 禁用
    async disabled(id, state) {
      const newState = state === 0 ? 1 : 0
      const res = await changeState({ id, state: newState })
      this.initList()
      console.log(res)
    },
    add() {
      this.isShowDialog = true
    },
    edit(id) {
      this.isShowEdit = true
      this.id = id
    },
    async remove(id) {
      const res = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (res !== 'confirm') return
      await remove({ id: id })
      this.$message.success('删除成功！')
      this.initList()
    }
  },
  filters: {
    dateFilter(val) {
      return dayjs(val).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style scoped lang="scss">
.top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}
.tag {
  display: flex;
  justify-content: space-between;
}
.el-pagination {
  text-align: right;
  margin-top: 20px;
}
.link {
  margin-right: 10px;
}
</style>
